package com.kfm.bootdemo.controller;

import com.kfm.bootdemo.model.Page;
import com.kfm.bootdemo.model.Student;
import com.kfm.bootdemo.service.StudentService;
import org.springframework.format.annotation.DateTimeFormat;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.servlet.ModelAndView;
import org.springframework.web.servlet.mvc.support.RedirectAttributes;

import javax.servlet.http.HttpServletRequest;
import java.util.Arrays;
import java.util.Date;
import java.util.List;

// @Controller 标识类中的方法可以接收请求，处理请求
// @RestController 标识类中的方法可以接收请求，处理请求，方法的是什么内容，响应给前端（）就是什么内容
@Controller
//@RestController
public class StudentController {

    private StudentService studentService = new StudentService();

    @RequestMapping("/student")
    public ModelAndView all() {
        ModelAndView mv = new ModelAndView();
        mv.setViewName("student");
        // 返回这个页面 thymeleaf 会去 templates 中找对应的html
        return mv;
    }

    // 参数 第几页 一页多少条

    /**
     *
     * @param pageNum 查询第几页
     * @param pageSize 每页的条数
     * @param student 查询的条件
     * @return view -- student  model page,condition
     */
    @RequestMapping("/listPage")
    public ModelAndView getPage(Integer pageNum, Integer pageSize, Student student) {
        // 总数/每页的数量
        Page<Student> page = studentService.findPage(pageNum, pageSize, student);
        ModelAndView mv = new ModelAndView();
        mv.addObject("page", page);
        System.out.println(Arrays.toString(page.getPageNumbers()));
        mv.addObject("condition", student);
        mv.setViewName("student");
        // 返回这个页面 thymeleaf 会去 templates 中找对应的html
        return mv;
    }

    @RequestMapping("/edit")
    public String edit(String id, String name, String birth, String gender, RedirectAttributes attributes) {
        int rows = studentService.editStudent(Integer.parseInt(id), name, birth, gender);
        if (rows > 0) {
            attributes.addFlashAttribute("msg", "修改成功！");
            return "redirect:/listPage";
        }
        attributes.addFlashAttribute("msg", "修改失败！");
        return "redirect:/listPage";
    }

    // 页面上输入的类型是字符串类型 但是接受的时候使用Date
    // 在Date的变量上加了一个注解@DateTimeFormat pattern 字符串的格式
    // student 参数的意义 将请求中的参数封装到 Student 对象中 参数名和对象属性名一致 类型也是一致的 （@DateTimeFormat(pattern = "yyyy-MM-dd")）
    @RequestMapping(value = "/selectByCondition", method = RequestMethod.GET)
    public ModelAndView selectByCondition(Student student) {
        List<Student> byCondition = studentService.findBYCondition(student);
        ModelAndView mv = new ModelAndView();
        mv.setViewName("student");
        Page<Student> studentPage = new Page<Student>(1, 5,
                (byCondition == null ? 0 : byCondition.size()), byCondition);
        System.out.println(studentPage.getPageCount());
        System.out.println(Arrays.toString(studentPage.getPageNumbers()));
        mv.addObject("page", studentPage);
        mv.addObject("condition", student);
        return mv;
    }
}
